renextop
.controller('AppCtrl',function($scope,WeixinService,$location,ActivityService,CONFIG,$ionicModal,$ionicPopup,PrizeService){
    $scope.getDate = function(time){
        return moment.unix(time/1000).format("YYYY-MM-DD");
      }

        $ionicModal.fromTemplateUrl('/static/templates/rule.html', {
            scope: $scope,
            animation: 'slide-in-up'
          }).then(function(modal) {
            $scope.ruleModal = modal;
          });

      $scope.openRuleModal = function() {
        $scope.ruleModal.show();
      };
      $scope.closeRuleModal = function() {
        $scope.ruleModal.hide();
      };

      $ionicModal.fromTemplateUrl('/static/templates/feedback.html', {
            scope: $scope,
            animation: 'slide-in-up'
          }).then(function(modal) {
            $scope.feedModal = modal;
          });
          $scope.openFeedModal = function() {
            $scope.feedModal.show();
          };
          $scope.closeFeedModal = function() {
            $scope.feedModal.hide();
          };

           $ionicModal.fromTemplateUrl('/static/templates/prize.html', {
            scope: $scope,
            animation: 'slide-in-up'
          }).then(function(modal) {
            $scope.prizeModal = modal;
          });
          $scope.openPrizeModal = function() {
            $scope.prizeModal.show();
          };
          $scope.closePrizeModal = function() {
            $scope.prizeModal.hide();
          };

     
    

    function GetQueryString(name)
        {
             var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
             var r = window.location.search.substr(1).match(reg);
             if(r!=null)return  unescape(r[2]); return null;
        }

    $scope.queryRecords = function(){
        var promise1 = new ActivityService.queryOne({event_id:$scope.event_id,activity_id:$scope.activity_id});
        promise1.then(function(data){
            $scope.currentActivitor = data.data.activity;
            $scope.helpers= data.data.helpers;
            if(data.data.activity.now_price == data.data.activity.dest_price){
                var promise = new PrizeService.query({event_id:$scope.event_id,user_id:data.data.activity.user_id._id});
                promise.then(function(data){
                    console.log(data);
                    if(data.code){
                        $scope.prize = data.data;
                    }
                },function(data){
                    console.log(data);
                })
            }
            //console.log(data)
        },function(data){
            //console.log('err')
        })
    }
    $scope.code = GetQueryString("code");
    $scope.event_id = GetQueryString("event_id");
    $scope.activity_id="";
    if(GetQueryString("activity_id")){
        $scope.activity_id= GetQueryString("activity_id");        
        $scope.queryRecords()
    }
    var promise = new WeixinService.syncserver({url:window.location.href});
    promise.then(function(data){
        //console.log(data)

        //console.log(wx)

        var promise1 = WeixinService.code2token({code:$scope.code})
        promise1.then(function(data1){
            //console.log(data1)
            $scope.currentUser = data1;
            $scope.user_id = data1._id;

            var promise2 = ActivityService.queryFromUser({event_id:$scope.event_id,user_id:$scope.user_id});
            promise2.then(function(data2){
                if(data2.code){
                    $scope.currentUserActivity = data2.data;
                    if(!$scope.activity_id){

                        $scope.activity_id=  $scope.currentUserActivity._id;        

                        $scope.queryRecords();
                    }
                }
                wx.config({
                      debug: false,
                      appId: data.appid,
                      timestamp: data.timestamp,
                      nonceStr: data.noncestr,
                      signature: data.signature,
                      jsApiList: [
                        'checkJsApi',
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage',
                      ]
                  });
            },function(data){
                //console.log('err')
            })
            //console.log(data1)

            
        },function(){
            //console.log('err')
        })
    },function(data){
        //console.log('err')
    })






    wx.ready(function(){
        if($scope.activity_id){
            $scope.shareUrl = encodeURIComponent('http://wechat.topxgun.com/activity?event_id='+$scope.event_id+'&activity_id='+$scope.activity_id);
        }else{
            $scope.shareUrl = encodeURIComponent('http://wechat.topxgun.com/activity?event_id='+$scope.event_id);
        }
        wx.onMenuShareTimeline({
            title: '双11搞机盛典 ｜  拿飞机，可劲搞，砍到千元抢植保飞控大礼包！', // 分享标题
            link: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+CONFIG.appid+'&redirect_uri='+$scope.shareUrl+'&response_type=code&scope=snsapi_userinfo&state=weixin#wechat_redirect', // 分享链接
            imgUrl: 'http://wechat.topxgun.com/static/img/main_top.png', // 分享图标
            success: function () { 
                // 用户确认分享后执行的回调函数
                //console.log('分享成功');
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
                //console.log('分享失败');
            }
        });

        wx.onMenuShareAppMessage({
            title: '双11搞机盛典 ｜  拿飞机，可劲搞，砍到千元抢植保飞控大礼包！', // 分享标题
            desc: '睡你MB起来嗨，双11搞机狂欢夜，植保大礼包已开启裸奔模式，你还不快来！', // 分享描述
            link: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+CONFIG.appid+'&redirect_uri='+$scope.shareUrl+'&response_type=code&scope=snsapi_userinfo&state=weixin#wechat_redirect', // 分享链接
            imgUrl: 'http://wechat.topxgun.com/static/img/main_top.png', // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () { 
                // 用户确认分享后执行的回调函数
                //console.log('分享成功')
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
                //console.log('分享失败')

            }
        });

        wx.checkJsApi({
            jsApiList: ['checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                ], // 需要检测的JS接口列表，所有JS接口列表见附录2,
            success: function(res) {
                // 以键值对的形式返回，可用的api值true，不可用为false
                // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                //console.log(res);
            }
        });
        
        $scope.showAlert = function(info) {
            alert(info)
        }

        $scope.cutPrice = function(){
            var data = {

                activity_id:$scope.activity_id,
                helper_id:$scope.user_id,
                help_method:1
            }
            //console.log($scope.activity_id)
            var promise = new ActivityService.helper(data);
            promise.then(function(data){
                //console.log(data)
                if(data.code){
                    
                    if(data.code==2){
                         //console.log(data)
                         $scope.prize = data.prize;
                         $scope.openPrizeModal(data.prize)
                    }else{

                        if(data.code==1 && $scope.currentActivitor.user_id._id == $scope.user_id){
                        $scope.au = data.au;
                        $scope.openFeedModal();   
                        }
                    }
                }
                $scope.queryRecords();
                $scope.showAlert(data.info)
            },function(data){
                //console.log('err')
            })
        }

        $scope.addPrice = function(){
            var data = {
                activity_id:$scope.activity_id,
                helper_id:$scope.user_id,
                help_method:2
            }
            //console.log($scope.activity_id)
            var promise = new ActivityService.helper(data);
            promise.then(function(data){
                //console.log(data)
                if(data.code){
                    if(data.code==2){
                        //console.log(data);
                    }else{
                      if(data.code==1 &&  $scope.currentActivitor.user_id._id == $scope.user_id){
                          $scope.au = data.au;
                      }  
                    }
                }
                $scope.queryRecords();
                $scope.showAlert(data.info)
            },function(data){
                //console.log('err')
            })
        }

        $scope.helpme = function(){
            var data = {
                event_id:$scope.event_id,
                user_id:$scope.user_id
            }
            $scope.prize = "";

            var promise = new ActivityService.create(data);
            promise.then(function(data){
                //console.log(data)
                if(data.code){
                    $scope.activity_id = data.data._id;
                    //console.log($scope.activity_id)
                    $scope.queryRecords();
                    // if(data.code ==2){
                    //     $scope.queryRecords();
                    // }

                    $scope.shareUrl = encodeURIComponent('http://wechat.topxgun.com/activity?event_id='+$scope.event_id+'&activity_id='+$scope.activity_id);
                    wx.onMenuShareTimeline({
                        title: '有人@我 ｜ 快来帮我砍价，植保飞控大礼包马上到手啦！', // 分享标题
                        link: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+CONFIG.appid+'&redirect_uri='+$scope.shareUrl+'&response_type=code&scope=snsapi_userinfo&state=weixin#wechat_redirect', // 分享链接
                        imgUrl: 'http://wechat.topxgun.com/static/img/main_top.png', // 分享图标
                        success: function () { 
                            // 用户确认分享后执行的回调函数
                            //console.log('分享成功');
                        },
                        cancel: function () { 
                            // 用户取消分享后执行的回调函数
                            //console.log('分享失败');
                        }
                    });

                    wx.onMenuShareAppMessage({
                        title: '有人@我 ｜ 快来帮我砍价，植保飞控大礼包马上到手啦！', // 分享标题
                        desc: '还差一点点，就可以抢到拓攻植保大礼包啦～', // 分享描述
                        link: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+CONFIG.appid+'&redirect_uri='+$scope.shareUrl+'&response_type=code&scope=snsapi_userinfo&state=weixin#wechat_redirect', // 分享链接
                        imgUrl: 'http://wechat.topxgun.com/static/img/main_top.png', // 分享图标
                        type: '', // 分享类型,music、video或link，不填默认为link
                        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                        success: function () { 
                            // 用户确认分享后执行的回调函数
                            //console.log('分享成功')
                        },
                        cancel: function () { 
                            // 用户取消分享后执行的回调函数
                            //console.log('分享失败')

                        }
                    });
                }
                $scope.showAlert(data.info)
            },function(data){
                //console.log('err')
            })
        }
    });

    
})
